package org.zyj.io.example.case7;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import org.zyj.io.example.codec.protobuf.MyData;

/**
 * 描述
 *
 * @author zhouyijin
 * @date 2022-01-14 16:29:33
 */
class ProtobufMultiMsgClientHandler extends ChannelInboundHandlerAdapter {

    //当通道就绪的时候, 会被调用
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("channel actived, ctx = " + ctx);
        var studentMsg = MyData.MyMessage.newBuilder().setDataType(MyData.MyMessage.DataType.StudentType).setStudent(MyData.Student.newBuilder().setId(99).setName("学生1").build());
        var teacherMsg = MyData.MyMessage.newBuilder().setDataType(MyData.MyMessage.DataType.TeacherType).setTeacher(MyData.Teacher.newBuilder().setId(99).setName("老师1").setCourse("语文").build());
        ctx.writeAndFlush(studentMsg);
//        ctx.writeAndFlush(teacherMsg);
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
//        super.channelRead(ctx, msg);
    }

    //异常处理
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.close();
    }
}
